//
//  HCPlaySteam.m
//  YOGProj
//
//  Created by liyun on 14-7-19.
//  Copyright (c) 2014年 qkong. All rights reserved.
//

#import "HCPlaySteam.h"
#import "AppDelegate.h"
#import <MediaPlayer/MediaPlayer.h>
#import "HCMacroDefinition.h"

@interface HCPlaySteam ()
{
    UIImageView *tip;
    int height;
    
    UIPanGestureRecognizer *pan;
}

@property (strong, nonatomic) MPMoviePlayerController *streamPlayer;
@property (weak,   nonatomic) IBOutlet UITableView    *listTable;
@end

@implementation HCPlaySteam

- (void)viewWillDisappear:(BOOL)animated
{
    [self remove];
    self.navigationController.navigationBarHidden = NO;
    app.flag = 0;
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [self startPlay];
    app.flag = 1;
}

- (void)navigationShowLeft
{
    NSLog(@"左边栏");
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = NSLocalizedString(@"HCCCTV",@"");
    
    height = MODEL?568:480;
    
    //    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //    button.frame     = CGRectMake(0, 0, 30, 30);
    //    [button setBackgroundImage:[UIImage imageWithContentsOfFile:[BUNDLE pathForResource:@"HCback" ofType:@"png"]] forState:UIControlStateNormal];
    //    [button addTarget:self action:@selector(navigationShowLeft) forControlEvents:UIControlEventTouchUpInside];
    //    UIBarButtonItem *barItemleft          = [[UIBarButtonItem alloc]initWithCustomView:button];
    //    self.navigationItem.leftBarButtonItem = barItemleft;
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(videoHasFinishedPlaying:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.streamPlayer];
    
    //    [self startPlay];
    
    pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHandler)];
    //    [self.view addGestureRecognizer:pan];
    
}

- (void)panHandler
{
    NSLog(@"dddd");
}

- (void)startPlay
{
    NSURL *streamURL = [NSURL URLWithString:HCCCTVURL];
    
    if(self.streamPlayer)
    {
        [self.streamPlayer.view removeFromSuperview];
    }
    self.streamPlayer = [[MPMoviePlayerController alloc] initWithContentURL:streamURL];
    [self.streamPlayer.view setFrame:CGRectMake(0, -64, 320, height)];
    self.streamPlayer.scalingMode = MPMovieScalingModeNone;
    self.streamPlayer.controlStyle = MPMovieControlStyleFullscreen;
    [self.view addSubview: self.streamPlayer.view];
    [self.streamPlayer play];
    
    //    if(!tip)
    //    {
    //        tip = [[UIImageView alloc] initWithFrame:CGRectMake(129, self.view.frame.size.height-50, 62, 36)];
    //        tip.alpha = 0.2;
    //        tip.image = [UIImage imageNamed:@"NTupAction.png"];
    //        [self.view addSubview:tip];
    //    }
    
    //    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHandler:)];
    //    [self.streamPlayer.view addGestureRecognizer:pan];
}

//- (IBAction)panHandler:(UIPanGestureRecognizer *)sender
//{
//    if(sender.state == UIGestureRecognizerStateChanged)
//    {
//        CGPoint offset = [sender translationInView:self.streamPlayer.view];
//
//        [self.streamPlayer.view setTransform:CGAffineTransformMakeTranslation(0, offset.y)];
//        [tip setTransform:CGAffineTransformMakeTranslation(0, offset.y)];
//        if(offset.y <= -100)
//        {
//            [UIView beginAnimations:nil context:nil];
//            [UIView setAnimationCurve:UIViewAnimationCurveLinear];
//            [UIView setAnimationDuration:0.5];
//            [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
//            [self.streamPlayer.view setTransform:CGAffineTransformMakeTranslation(0, -self.view.frame.size.height)];
//            [tip setTransform:CGAffineTransformMakeTranslation(0, -self.view.frame.size.height)];
//            [UIView commitAnimations];
//            [self performSelector:@selector(remove) withObject:self afterDelay:0.5];
//        }
//        else
//        {
//            [UIView beginAnimations:nil context:nil];
//            [UIView setAnimationCurve:UIViewAnimationCurveLinear];
//            [UIView setAnimationDuration:0.5];
//            [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES];
//            [self.streamPlayer.view setTransform:CGAffineTransformMakeTranslation(0, 0)];
//            [tip setTransform:CGAffineTransformMakeTranslation(0, 0)];
//            [UIView commitAnimations];
//        }
//    }
//}

- (void)remove
{
    if(self.streamPlayer)
    {
        [self.streamPlayer.view removeFromSuperview];
    }
    [self.streamPlayer stop];
    self.streamPlayer = nil;
    
    if(tip)
    {
        [tip removeFromSuperview];
        tip = nil;
    }
}

//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    return listArray.count;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 60;
//}
//
//-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    static NSString *CellIdentifier = @"cell";
//
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//
//    if (cell == nil)
//    {
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//    }
//
//    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 59, 320, 1)];
//    image.backgroundColor = [UIColor colorWithRed:189/255.0 green:189/255.0 blue:189/255.0 alpha:1.0f];
//    [cell.contentView addSubview:image];
//
//    cell.textLabel.text = titleArray[indexPath.row];
//    cell.textLabel.textColor = [UIColor whiteColor];
//    cell.textLabel.font = [UIFont boldSystemFontOfSize:16.0f];
//    cell.backgroundColor = [UIColor clearColor];
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    return cell;
//}
//
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    NSURL *streamURL = [NSURL URLWithString:listArray[indexPath.row]];
//
//    if(self.streamPlayer)
//    {
//        [self.streamPlayer.view removeFromSuperview];
//    }
//
//    self.streamPlayer = [[MPMoviePlayerController alloc] initWithContentURL:streamURL];
//    [self.streamPlayer.view setFrame:self.view.bounds];
//    self.streamPlayer.controlStyle = MPMovieControlStyleNone;
//
//    [self.view addSubview: self.streamPlayer.view];
//    [self.streamPlayer play];
//
//    if(!tip)
//    {
//        tip = [[UIImageView alloc] initWithFrame:CGRectMake(129, self.view.frame.size.height-50, 62, 36)];
//        tip.alpha = 0.2;
//        tip.image = [UIImage imageNamed:@"NTupAction.png"];
//        [self.view addSubview:tip];
//    }
//
//    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHandler:)];
//    [self.streamPlayer.view addGestureRecognizer:pan];
//}


- (void)videoHasFinishedPlaying:(NSNotification *)paramNotification{
    /* Find out what the reason was for the player to stop */
    NSNumber *reason =
    [paramNotification.userInfo
     valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
    if (reason != nil){
        NSInteger reasonAsInteger = [reason integerValue];
        switch (reasonAsInteger){
            case MPMovieFinishReasonPlaybackEnded:{
                /* The movie ended normally */
                break; }
            case MPMovieFinishReasonPlaybackError:{
                /* An error happened and the movie ended */
                break;
            }
            case MPMovieFinishReasonUserExited:{
                /* The user exited the player */
                break;
            }
        }
        NSLog(@"Finish Reason = %ld", (long)reasonAsInteger);
    } /* if (reason != nil){ */
    
    [self remove];
    
    //    [self startPlay];
    //    NSURL *streamURL = [NSURL URLWithString:HCCCTVURL];
    //    if(self.streamPlayer)
    //    {
    //        [self.streamPlayer.view removeFromSuperview];
    //    }
    //    self.streamPlayer = [[MPMoviePlayerController alloc] initWithContentURL:streamURL];
    //    if(self.streamPlayer)
    //        [self.streamPlayer.view setFrame:CGRectMake(0, 0, height, 320)];
    //    self.streamPlayer.scalingMode = MPMovieScalingModeAspectFill;
    //    self.streamPlayer.controlStyle = MPMovieControlStyleFullscreen;
    //    [self.view addSubview: self.streamPlayer.view];
    //    [self.streamPlayer play];
    
    //    [self navigationShowLeft];
    self.navigationController.navigationBarHidden = YES;
}


- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
    
    switch (interfaceOrientation) {
        case UIInterfaceOrientationPortrait:
            //home健在下
        {
            NSLog(@"下");
            self.streamPlayer.controlStyle = MPMovieControlStyleFullscreen;
            [self.view removeGestureRecognizer:pan];
            self.navigationController.navigationBarHidden = NO;
            
            if(self.streamPlayer)
            {
                self.streamPlayer.view.frame = CGRectMake(0, -64, 320, height);
            }
        }
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            //home健在上
        {
            NSLog(@"上");
            self.streamPlayer.controlStyle = MPMovieControlStyleFullscreen;
            [self.view removeGestureRecognizer:pan];
            self.navigationController.navigationBarHidden = NO;
            if(self.streamPlayer)
            {
                self.streamPlayer.view.frame = CGRectMake(0, -64, 320, height);
            }
            
        }
            break;
        case UIInterfaceOrientationLandscapeLeft:
            //home健在左
        {
            self.navigationController.navigationBarHidden = YES;
            self.streamPlayer.controlStyle = MPMovieControlStyleNone;
            [self.view addGestureRecognizer:pan];
            if(self.streamPlayer)
            {
                [self.streamPlayer.view setFrame:CGRectMake(0, 0, height, 320)];
            }
            NSLog(@"左");
        }
            break;
        case UIInterfaceOrientationLandscapeRight:
            //home健在右
        {
            NSLog(@"右");
            self.streamPlayer.controlStyle = MPMovieControlStyleNone;
            [self.view addGestureRecognizer:pan];
            
            if(self.streamPlayer)
            {
                [self.streamPlayer.view setFrame:CGRectMake(0, 0, height, 320)];
            }
            self.navigationController.navigationBarHidden = YES;
            
        }
            break;
        default:
            NSLog(@"default");
            
            self.navigationController.navigationBarHidden = NO;
            if(self.streamPlayer)
            {
                self.streamPlayer.view.frame = CGRectMake(0, -64, 320, height);
            }
            break;
    }
}

-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
